package cn.xgxd.interceptor;

import cn.hutool.core.util.StrUtil;
import cn.xgxd.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserInfolnterceptor implements HandlerInterceptor {
    @Autowired
    UserContext userContext ;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取请求头
        String userInfo = request.getHeader("userInfo");
        //2.判断
        if (StrUtil.isNotBlank(userInfo)){
            String userId = getUser(userInfo);
            userContext.setUserId(userId);
        }
        //3.放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        userContext.removeUserId();
    }

    public String getUser(String user){
        String id = user.substring(4, user.indexOf(","));
        return id;
    }
}
